package com.graduate.after.service.impl;

import com.graduate.after.bean.UserInfo;
import com.graduate.after.mapper.UserInfoMapper;
import com.graduate.after.service.UserInfoService;
import com.graduate.after.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void registerService(String username, String password, Long phone) {
        UserInfo userInfo = new UserInfo();
        IdWorker idWorker = new IdWorker();
        long id = idWorker.nextId();
        // 雪花算法算出来的是19位数字,js数字最长为16位，故截取三位
        id = Long.parseLong(String.valueOf(id).substring(3, String.valueOf(id).length()));
        userInfo.setId(id);
        userInfo.setUsername(username);
        userInfo.setPasseord(password);
        userInfo.setPhone(phone);
        userInfoMapper.registerDao(userInfo);
    }

    @Override
    public UserInfo loginService(String username, String password) {
        UserInfo userInfo = userInfoMapper.loginDao(username);
        if(userInfo != null) {
            return Objects.equals(password,userInfo.getPasseord())?userInfo:null;
        }
        return userInfo;
    }

    @Override
    public String judgeService(String username) {
        UserInfo userInfo = userInfoMapper.loginDao(username);
        return userInfo == null?"Yes":"No";
    }

    @Override
    public String selectUserNameById(Long id) {
        String userName = userInfoMapper.selectUserNameById(id);
        return userName;
    }
}
